package org.nfIt.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.nfIt.dto.h5.ProductSkuDto;
import org.nfIt.entity.product.Product;
import org.nfIt.entity.product.ProductDetails;
import org.nfIt.entity.product.ProductSku;
import org.nfIt.mapper.ProductDetailsMapper;
import org.nfIt.mapper.ProductMapper;
import org.nfIt.mapper.ProductSkuMapper;
import org.nfIt.service.ProductService;
import org.nfIt.vo.h5.ProductItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClossName:ProductServiceImpl
 * Pakage:org.nfIt.service.impl
 * Description
 *h5商品实现类
 * @Author L
 * @Create 2024/9/18 20:20
 * @Version 2023.1.3
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;


    //查询热销商品，根据sale_num查询前10条
    @Override
    public List<ProductSku> findProductSkuBySale() {
        return productSkuMapper.findProductSkuBySale();
    }

    /**
     * 分页查询商品
     * @param page 当前页
     * @param limit 每页记录数
     * @param productSkuDto 条件
     */
    @Override
    public PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page, limit);
        List<ProductSku> productSkuList = productSkuMapper.findByPage(productSkuDto);
        return new PageInfo<>(productSkuList);
    }

    /**
     * 查询商品详情
     * @param skuId
     */
    @Override
    public ProductItemVo item(Long skuId) {

        ProductItemVo productItemVo = new ProductItemVo();

        //1)根据skuId获取sku信息
        ProductSku productSku = productSkuMapper.getById(skuId);

        //2)从sku信息中获取商品id,根据商品id查询商品信息
        Product product = productMapper.getById(productSku.getProductId());


        //3)根据从sku信息中获取商品id，查询商品详情信息
        ProductDetails productDetails = productDetailsMapper.getByProductId(productSku.getProductId());

        //4)根据商品id查询同一个商品下面的sku信息列表
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(productSku.getProductId());
        //建立sku规格与skuId对应关系
        Map<String,Object> skuSpecValueMap = new HashMap<>();
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(),item.getId());
        });


        //5)创建vo对象封装最终数据
        //商品sku信息
        productItemVo.setProductSku(productSku);

        //商品信息
        productItemVo.setProduct(product);

        //封装sku规格与skuId对应关系的map集合
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);

        //商品封装详情图片
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));

        //封装轮播图信息
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        System.out.println();
        //封装商品规格信息
        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));

        return productItemVo;

    }
}
